package com.airport.ticket.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.airport.ticket.common.UrlUtil;
import com.airport.ticket.dao.FlightInfoMapper;
import com.airport.ticket.entity.FlightInfo;
import com.airport.ticket.entity.FlightInfoExample;
import com.airport.ticket.entity.UserInfo;
import com.airport.ticket.entity.UserInfoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/admin")
public class FlightController {

	@Autowired
	private FlightInfoMapper flightInfoMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(value = "/flight_list.html")
    public String flightList(ModelMap map, String keyword, String pageNow, String pageSize, String errorMsg){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "航班管理");
		map.addAttribute("keyword", keyword);
		
		if(errorMsg!=null && !"".equals(errorMsg)) {
			map.addAttribute("errorMsg", errorMsg);
		}
		
		if(pageNow==null || "".equals(pageNow) || Integer.parseInt(pageNow) < 1) {
			pageNow = "1";
		}
		
		if(pageSize==null || "".equals(pageSize) || Integer.parseInt(pageSize) < 1) {
			pageSize = "20";
		}
		
		FlightInfoExample condition = new FlightInfoExample();
		if(keyword!=null && !"".equals(keyword)) {
			condition.createCriteria().andFlightNumberLike("%"+keyword+"%");
		}
		condition.setOrderByClause("id DESC");
		PageHelper.startPage(Integer.parseInt(pageNow),Integer.parseInt(pageSize));
		List<FlightInfo> resultList = flightInfoMapper.selectByExample(condition);
		
		if(resultList==null || resultList.size()==0){
			map.addAttribute("retCode", "99");
			map.addAttribute("pageNow", pageNow);
			map.addAttribute("pageSize", pageSize);
			map.addAttribute("totalPage", "0");
			return "admin/flight/flight_list";
        }
		
		PageInfo<FlightInfo> pageInfo = new PageInfo<>(resultList);
		map.addAttribute("pageNow", pageNow);
		map.addAttribute("pageSize", pageSize);
		map.addAttribute("totalPage", pageInfo.getPages());
		map.addAttribute("resultList", resultList);
			
        return "admin/flight/flight_list";
    }
	
	@RequestMapping(value = "/add_flight.html")
    public String addflight(ModelMap map){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "新增航班");		
		return "admin/flight/add_flight";
    }
	

	@PostMapping(value = "/do_add_flight.html")
    public String doAddflight(ModelMap map, FlightInfo flight){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "新增航班");	
		
		/**
		 * 判断航班号是否重复
		 */
		FlightInfoExample condition = new FlightInfoExample();
		condition.createCriteria().andFlightNumberEqualTo(flight.getFlightNumber());
		List<FlightInfo> flightInfoList = flightInfoMapper.selectByExample(condition);
		if(flightInfoList!=null && flightInfoList.size() > 0) {
			return "redirect:/admin/flight_list.html?errorMsg="+UrlUtil.parseUtf8("航班号已存在，请更换");
		}
		
		/**
		 * 插入航班表
		 */
		flight.setCreateTime(new Date());
		flightInfoMapper.insertSelective(flight);
		
		return "redirect:/admin/flight_list.html";
    }
	
	@RequestMapping(value = "/update_flight.html")
    public String updateflight(ModelMap map,String id){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "编辑航班");	
		
		/**
		 * 判断航班是否存在
		 */
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(flightInfo==null || flightInfo.getId()==null || "".equals(flightInfo.getId())) {
			return "redirect:/admin/flight_list.html?errorMsg="+UrlUtil.parseUtf8("航班不存在");
		}
		
		map.addAttribute("flightInfo", flightInfo);		
		return "admin/flight/update_flight";
    }
	

	@PostMapping(value = "/do_update_flight.html")
    public String doUpdateflight(ModelMap map, FlightInfo flight){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "编辑航班");
		
		/**
		 * 判断航班是否存在
		 */
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(flight.getId());
		if(flightInfo==null || flightInfo.getId()==null || "".equals(flightInfo.getId())) {
			return "redirect:/admin/flight_list.html?errorMsg="+UrlUtil.parseUtf8("航班不存在");
		}
		
		/**
		 * 判断航班号是否重复
		 */
		FlightInfoExample condition = new FlightInfoExample();
		condition.createCriteria().andFlightNumberEqualTo(flight.getFlightNumber());
		List<FlightInfo> flightInfoList = flightInfoMapper.selectByExample(condition);
		if(flightInfoList!=null && flightInfoList.size() > 0 &&  !flightInfoList.get(0).getId().equals(flight.getId())) {
			return "redirect:/admin/flight_list.html?errorMsg="+UrlUtil.parseUtf8("航班号已存在，请更换");
		}
		
		/**
		 * 更新航班表
		 */
		flightInfoMapper.updateByPrimaryKeySelective(flight);

		return "redirect:/admin/flight_list.html";
    }
	

	@RequestMapping(value = "/do_delete_flight.html")
    public String doDeleteflight(ModelMap map, FlightInfo flight){
		map.addAttribute("nav", "2");
		map.addAttribute("title", "删除航班");
		
		/**
		 * 判断航班是否存在
		 */
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(flight.getId());
		if(flightInfo==null || flightInfo.getId()==null || "".equals(flightInfo.getId())) {
			return "redirect:/admin/flight_list.html?errorMsg="+UrlUtil.parseUtf8("航班不存在");
		}
		
		
		/**
		 * 删除航班
		 */
		flightInfoMapper.deleteByPrimaryKey(flight.getId());

		return "redirect:/admin/flight_list.html";
    }
	
}
